package com.windowes.weichang.service;

import com.google.common.collect.Lists;
import com.windowes.weichang.common.ServerResponse;
import com.windowes.weichang.dao.SysDeptMapper;
import com.windowes.weichang.dao.WDistrictMapper;
import com.windowes.weichang.dto.DeptIdDTO;
import com.windowes.weichang.dto.DeptIdParamDTO;
import com.windowes.weichang.pojo.SysDept;
import com.windowes.weichang.pojo.WDistrict;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/***
 *
 * @author: yangqing
 * @email: qing.yang@bangdao-tech.com
 * @createTime: 2018/7/29 下午10:35
 * @description:
 *
 **/
@Service
public class DeptService {


    @Autowired
    WDistrictMapper wDistrictMapper;

    @Autowired
    SysDeptMapper sysDeptMapper;

    /**
     * @param deptIdParamDTO
     * @return
     */
    public ServerResponse getDeptId(DeptIdParamDTO deptIdParamDTO) {
        String city = deptIdParamDTO.getCity();
        List<SysDept> deptList = Lists.newArrayList();
        DeptIdDTO deptIdDTO = new DeptIdDTO();
        if (!StringUtils.isBlank(city)) {
            WDistrict district = new WDistrict();
            district.setName(deptIdParamDTO.getCity());
            district.setLevel(1);
            WDistrict currentDistrict = wDistrictMapper.selectByName(district);
            if (currentDistrict != null) {
                SysDept dept = sysDeptMapper.selectByProvinceId(currentDistrict.getId());
                if (dept != null) {
                    deptIdDTO.setDeptId(dept.getId());
                    return ServerResponse.createBySuccess(deptIdDTO);
                }
            }
            // 2018/7/29 模糊查询
            List<WDistrict> districts = wDistrictMapper.selectByLikeName(district);
            if (!districts.isEmpty()) {
                for (WDistrict likeDistrict : districts) {
                    SysDept dept = sysDeptMapper.selectByProvinceId(likeDistrict.getId());
                    if (dept != null) {
                        deptList.add(dept);
                    }
                }
                if (!deptList.isEmpty()) {
                    deptIdDTO.setRecommandList(deptList);
                    return ServerResponse.createBySuccess(deptIdDTO);
                }

            }

        }

        // TODO: 2018/7/29 经纬度查询(高德地图距离查询能力有限,待定)

        //查询所有代理
        getList(deptList);
        deptIdDTO.setRecommandList(deptList);
        return ServerResponse.createBySuccess(deptIdDTO);

    }

    private void getList(List<SysDept> deptList) {
        List<SysDept> depts = sysDeptMapper.selectList();
        for (SysDept dept : depts) {
            if (dept.getId() == 1 || dept.getId() == 2) continue;
            deptList.add(dept);
        }
    }

    public ServerResponse list() {
        List<SysDept> deptList = Lists.newArrayList();
        getList(deptList);
        return ServerResponse.createBySuccess(deptList);
    }
}
